﻿@using System.Data
@using STX.Domain
@model STX.Admin.Models.BizActivity.EditQuoteModel
@{
    ViewBag.Title = "Add";
    Layout = null;
    STX.Domain.TabAdministrator CurUser = ViewBag.CurUser;
    var i = -1;
    var disabled = " disabled=\"disabled\"";
}
<style>
    #editquoteform .tempQPT{ position: absolute; width: 200px; z-index: 9; display: none;}
    #editquoteform .tempQPT ul{ background: #fff;padding: 5px;border: #333333 1px solid;}
    #editquoteform .tempQPT ul li{ border-bottom: #eee 1px solid;padding: 5px; cursor: pointer }
    #editquoteform .tempQPT ul li:hover{ background:#f0e68c}
    #editquoteform .tempQPT ul li.active{ background:#f0e68c}

    #editquoteform .tempQuotePrice{ position: absolute; width: 200px; z-index: 9; display: none;}
    #editquoteform .tempQuotePrice ul{ background: #fff;padding: 5px;border: #333333 1px solid;}
    #editquoteform .tempQuotePrice ul li{ border-bottom: #eee 1px solid;padding: 5px; }
</style>

<form class="pageForm addpage" action="@Url.Action("saveauditquote")" method="post" id="editquoteform">
    <div style=" height:100%; width:100%; overflow:hidden" class="verticalSplitContainer">
        <div class="pageFormContent" ratio="*">
            <input type="hidden" value="@Model.Id" name="Id" />
            <input type="hidden" value="@Model.AreaId" name="AreaId" />
            <input type="hidden" value="@Model.ProjectId" name="ProjectId" />
            <div class="inputActivityDiv">
                    <table style="width:100%">
                        <tr>    
                            <td width="15%">活动名称：</td>
                            <td width="35%"><input name="Name" class="textInput" type="text" size="40" alt="" value="@Model.Name"@disabled/></td>
                            <td width="15%">所属项目：</td>
                            <td width="35%">@Model.ProjectName
                            </td>
                        </tr>
                        <tr>    
                            <td width="15%">活动时间：</td>
                            <td width="35%"> <input name="StartTime" class="textInput datetime" type="text" size="8" value="@Model.StartTime.ToString("yyyy-M-d")" alt=""@disabled/> - <input name="EndTime" class="textInput datetime" type="text" size="8" value="@Model.EndTime.ToString("yyyy-M-d")" alt=""@disabled/> <input name="TimeDetail" class="textInput" type="text" size="16" value="@Model.TimeDetail"  alt=""@disabled/></td>
                            <td width="15%">活动地点：</td>
                            <td width="35%"><input name="Place" class="textInput" type="text" size="30" value="@Model.Place" alt=""@disabled/></td>
                        </tr>
                        <tr>    
                            <td width="15%">报价制作人：</td>
                            <td width="35%"><input name="QuotationTabulator" class="textInput" type="text" size="30" value="@Model.QuotationTabulator" @disabled  alt=""/></td>
                            <td width="15%">制表时间：</td>
                            <td width="35%"><input name="QuotationGeneratedTime" class="textInput datetime" type="text" size="15" value="@Model.QuotationGeneratedTime.ToString("yyyy-MM-dd")" disabled="disabled"  alt=""/></td>
                        </tr>
                        @*
                        <tr>    
                            <td width="15%">成本制表人：</td>
                            <td width="35%"><input name="CostTabulator" class="textInput" type="text" size="30"  alt=""/></td>
                            <td width="15%">制表时间：</td>
                            <td width="35%"><input name="CostGeneratedTime" class="textInput datetime" type="text" size="15" value="" alt=""/></td>
                        </tr>*@
                        <tr>
                            <td width="15%">执行：</td>
                            <td width="35%"><input name="Executor" class="textInput" type="text" size="30"  value="@Model.Executor" alt=""@disabled/></td>    
                            <td width="15%">负责人：</td>
                            <td width="35%"><input name="ResponsiblePerson" class="textInput" type="text"  value="@Model.ResponsiblePerson" size="30"  alt=""@disabled/></td>
                        </tr>
                        <tr>    
                            <td width="15%">策划：</td>
                            <td width="35%"><input name="Planner" class="textInput" type="text" size="30"  value="@Model.Planner" alt=""@disabled/></td>
                            <td width="15%">司机：</td>
                            <td width="35%"><input name="Driver" class="textInput" type="text" size="30"  value="@Model.Driver" alt=""@disabled/></td>
                        </tr>
                    </table>
                    <table style="width: 100%">
                        
                        <tr style="opacity: 0;">
                            <td style="width:100px;"><input name="" style="width:100px;" class="textInput" /></td>
                            <td style="width:200px;"><input name="" class="textInput" type="text" style="width:200px;"/> </td>
                            <td style="width:50px;"><input name="" class="textInput" type="text" style="width:50px;"/></td>
                            <td style="width:50px;"><input name="" class="textInput" type="text" style="width:50px;"/></td>
                            <td style="width:50px;"><input name="" class="textInput" type="text" style="width:50px;"/></td>
                            <td style="width:50px;">@(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialFormEnum>(0, "select"))</td>
                            <td style="width:50px;">@(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialBelongEnum>(0, "select"))</td>
                        </tr>
                        <tr>
                            <td style="width:100px;"><h6 style="width:90px;">项目</h6></td>
                            <td style="width:200px;"><h6 style="width:190px;">具体要求</h6></td>
                            <td style="width:50px;"><h6 style="width:50px;">单位</h6></td>
                            <td style="width:50px;"><h6 style="width:50px;">数量</h6></td>
                            <td style="width:50px;"><h6 style="width:50px;">报价</h6></td>
                            <td style="width:50px;"><h6 style="width:50px;">物料形式</h6></td>
                            <td style="width:50px;"><h6 style="width:50px;">物料归属</h6></td>
                        </tr>
                    </table>
                   @for (var j = 0; j < Model.MaterialCategories.Count; j++)
                   {
                       var item = Model.MaterialCategories[j];
                       var activityItems = Model.BizActivityItems.Where(o => o.MaterialCategoryId == item.Id);
                        <div class="materialitem">
                            <h4 style=" background: #c0c0c0; padding: 6px;">@item.Name</h4>
                            <table style="width: 100%" id="activitytb(@item.Id)">
                            @{
                                var bizActivityItems = activityItems as BizActivityItem[] ?? activityItems.ToArray();
                             }
                            @if (!bizActivityItems.Any())
                            {
                                i++;
                                <tr>
                                    <td style="width:100px;" style="position: relative"><input onkeyup="getTemplateData(event,this)" name="BizActivityItems[@i].Name" style="width:100px;" class="textInput" type="text"/></td>
                                    <td style="width:200px;"><input name="BizActivityItems[@i].Require" class="textInput" type="text" style="width:200px;"/> </td>
                                    <td style="width:50px;"><input name="BizActivityItems[@i].Unit" class="textInput" type="text" style="width:50px;" /></td>
                                    <td style="width:50px;" style="position: relative"><input name="BizActivityItems[@i].Numb" class="textInput" type="text" style="width:50px;" value="0" onkeyup="calculate(this)" onfocus="showQuotePrice(this)"  onblur="hideQuotePrice(this)"/></td>
                                    <td style="width:50px;"><input name="BizActivityItems[@i].QuotationPrice" class="textInput" type="text" style="width:50px;" value="0" alt=""/></td>
                                    <td style="width:50px;">@(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialFormEnum>(0, "BizActivityItems[" + @i + "].MaterialForm"))</td>
                                    <td style="width:50px;">@(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialBelongEnum>(0, "BizActivityItems[" + @i + "].MaterialBelong")) <input type="hidden" name="BizActivityItems[@i].QuotePriceId" value="0"/><input type="hidden" name="BizActivityItems[@i].MaterialCategoryId" value="@item.Id"/></td>
                                </tr>
                            }
                            else
                            {
                                foreach (var activityItem in bizActivityItems)
                                {
                                    i++;
                                    var curColor = "";
                                    var curReadonly = "";
                                    if (activityItem.ErrorStatus == BizActivityItem.ErrorStatusEnum.Error)
                                    {
                                        curColor = " style=background:#ff3300";
                                    }
                                    if (activityItem.QuotePriceId > 0)
                                    {
                                        curReadonly = " readonly=readonly";
                                    }
        <tr>
                                    <td style="width:100px;" style="position: relative"><input onkeyup="getTemplateData(event,this)" name="BizActivityItems[@i].Name" value="@activityItem.Name" style="width:100px;" class="textInput" type="text"/></td>
                                    <td style="width:200px;"><input name="BizActivityItems[@i].Require" class="textInput" type="text" style="width:200px;"  value="@activityItem.Require"/> </td>
                                    <td style="width:50px;"><input name="BizActivityItems[@i].Unit" class="textInput" type="text" style="width:50px;"  value="@activityItem.Unit"/></td>
                                    <td style="width:50px;" style="position: relative"><input name="BizActivityItems[@i].Numb" class="textInput" type="text" style="width:50px;"  onkeyup="calculate(this)" onfocus="showQuotePrice(this)"  onblur="hideQuotePrice(this)"  value="@activityItem.Numb"/></td>
                                    <td style="width:50px;"><input name="BizActivityItems[@i].QuotationPrice" class="textInput" type="text" style="width:50px;" alt=""  value="@activityItem.QuotationPrice"/></td>
                                    <td style="width:50px;">@(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialFormEnum>((int)activityItem.MaterialForm, "BizActivityItems[" + @i + "].MaterialForm"))</td>
                                    <td style="width:50px;">@(Html.DropDownListFromEnum<STX.Domain.BizActivityItem.MaterialBelongEnum>((int)activityItem.MaterialBelong, "BizActivityItems[" + @i + "].MaterialBelong")) <input type="hidden" name="BizActivityItems[@i].QuotePriceId" value="@activityItem.QuotePriceId"/><input type="hidden" name="BizActivityItems[@i].MaterialCategoryId" value="@item.Id"/></td>
                                </tr>
                                }
                            }

                            </table>
                            <div style="padding: 0 0 10px 0"><a href="javascript:;" title="添加一行" class="addtr" style="font-size: 16px; font-weight: bold; text-decoration: none; color: #228b22">+</a>&nbsp;&nbsp;<a href="javascript:;" title="删去一行" class="removetr" style="font-size: 16px; font-weight: bold; text-decoration: none; color:crimson">-</a></div>
                        </div>
                   }
                </div>
        </div>

        <div class="formBar" ratio="#30">
            <ul>
                <li><div class="simplebutton"><div class="r"><button type="button" class="submit">提交</button></div></div></li>
                @Html.Partial("~/Administrator/Views/BizActivity/Step.cshtml", new STX.Admin.Models.BizActivity.StepModel(Model.Step))
            </ul>
        </div>
    </div>
    <script type="text/javascript">
        function SetInput($td) {
            var $tr = $td.parent();
            var data = $tr.attr("data");
            if (!data) {
                $tr.find("input[name*='Require']").val('');
                $tr.find("input[name*='Unit']").removeAttr("readonly").val('');
                $tr.find("input[name*='QuotationPrice']").removeAttr("readonly").val('');
                $tr.find("input[name*='QuotePriceId']").val('0');
                $("#editquoteform input").disabledinput();
                return;
            }
            var json = JSON.parse(data);
            
            $tr.find("input[name*='Name']").val(json.Name);
            $tr.find("input[name*='Require']").val(json.Required);
            $tr.find("input[name*='Unit']").attr("readonly", "readonly").val(json.Unit);
            $tr.find("input[name*='QuotationPrice']").attr("readonly", "readonly").val('0');
            showQuotePrice($tr.find("input[name*='Numb']").get());
            $tr.find("input[name*='Numb']").select();
            $("#editquoteform input").disabledinput();
        }


        function showQuotePrice(obj) {
            var $this = $(obj);
            var $td = $this.parent();
            var $tr = $this.parent().parent();
            var data = $tr.attr("data");
            if (!data) {
                return;
            }
            var json = JSON.parse(data);
            var $quotePrice = $td.find(".tempQuotePrice");
            if ($quotePrice.size() == 0) {
                $("<div class='tempQuotePrice'><ul></ul></div>").appendTo($td);
            }
            var $box = $td.find(".tempQuotePrice");
            $box.show().find("ul").empty();
            $.each(json.QuotePrices, function(i, d) {
                var $li = $('<li>数目：' + d.StartNum + ' 至 ' + d.EndNum + ' 报价：' + d.QuotePrice + '</li>');
                $box.find("ul").append($li);
            });
        }

        function hideQuotePrice(obj) {
            var $this = $(obj);
            var $td = $this.parent();
            $td.find(".tempQuotePrice").hide();
            calculate(obj);
        }

        function calculate(obj) {
            var $this = $(obj);
            var val = $this.val();
            if (!val.isNumber) {
                alert('需输入纯数字');
                return;
            }
            
            var $td = $this.parent();
            var $tr = $td.parent();
            var data = $tr.attr("data");
            if (!data)return;
            var json = JSON.parse(data);
            $.each(json.QuotePrices, function(i, d) {
                if (val <= d.EndNum && val >= d.StartNum) {
                    $tr.find("input[name*='QuotePriceId']").val(d.Id);
                    $tr.find("input[name*='QuotationPrice']").attr("readonly", "readonly").val(d.QuotePrice);
                    if (d.ProjectCostPrice) {
                        $tr.find("input[name*='ProjectCostPrice']").attr("readonly", "readonly").val(d.ProjectCostPrice);
                    }
                    if (d.FinanceCostPrice) {
                        $tr.find("input[name*='FinanceCostPrice']").attr("readonly", "readonly").val(d.FinanceCostPrice);
                    }
                }
            });     
           
        }

        function getTemplateData(e, obj) {

//            if (e.keyCode == 38) {
//                keyUp(obj);
//                return;
//            }
//            if (e.keyCode == 40) {
//                keyDown(obj);
//                return;
//            }
//            if (e.keyCode == 13) {
//                keyEnter(obj);
//                return;
//            }

            var $this = $(obj);
            var $td = $this.parent();
            var val = $this.val();
            if (!val) {
                $td.parent().removeAttr("data");
                $td.find(".tempQPT").remove();
                SetInput($td);
                return;
            }

            if (val == $this.attr("oldvalue")) {
                return;
            }
            $this.attr("oldvalue", val);
            var data = { key: val,areaId:$("#editquoteform input[name='AreaId']").val()};
            var option = {
                url: "@Url.Action("GetQuotePriceTemplateData")",
                data: data,
                type: "POST",
                dataType: "json",
                success: function(json) {
                    if (!json.success) {
                        $td.parent().removeAttr("data");
                        $td.find(".tempQPT").remove();
                        SetInput($td);
                        return;
                    } else {
                        $td.parent().attr("data", JSON.stringify(json.list));
                        SetInput($td);
                        return; 
                    }
                }
            };
            $.ajax(option);


        }


        $(document).bind("click", function(e) {
            var target = $(e.target);
            if (target.closest("#editquoteform .tempQPT").length == 0) {
                $("#editquoteform .tempQPT").hide();
            }
        });

        $(function() {

            $("#editquoteform input").disabledinput();
                
            function AddOrRemoveCallback($thisarea) {
                $("table tr", "#editquoteform .materialitem").each(function (i) {
                    $(this).find("input,select").each(function () {
                        var $thisinput = $(this);
                        $thisinput.attr("name", $thisinput.attr("name").replace(/\d+/g, i));
                    });
                });
            }

            $("#editquoteform .addtr").click(function (e) {
                var $thisMaterialItem = $(this).parentsUntil(".materialitem").parent();
                var $tr = $("table tr:last", $thisMaterialItem).clone();
                $tr.appendTo($("table", $thisMaterialItem));
                AddOrRemoveCallback($thisMaterialItem);
                $thisMaterialItem.find("tr:last").find("input:first").select();
            });

            $("#editquoteform .removetr").click(function () {
                var $thisMaterialItem = $(this).parentsUntil(".materialitem").parent();
                if ($("tr", $thisMaterialItem).size() > 1) {
                    $("tr:last", $thisMaterialItem).remove();
                    AddOrRemoveCallback($thisMaterialItem);
                }
            });
        });
    </script>
</form>